<? if (!defined("BASEPATH")) exit("No direct script access allowed");

if(isset($getEnt2cat)) {
	//Common::pr($getEnt2cat);
	//Common::pr($getEnt2image);

	$page["sideinfo"] = array();
	$page["summary"] = array();
	$page["details"] = array();

	// SCH adatok befűzése
	if($ENTITY == "SCH" and isset($getScholarship)) {
		if($getScholarship[0]["schSubName"]) {
			$getEnt2cat[$this->lang->line("STUDYING")] = array(
				"e2cText" => $getScholarship[0]["schSubName"],
				"e2cInt" => NULL,
				"catName" => $this->lang->line("STUDYING"),
				"catType" => "C",
			);
		}
		if(isset($getScholarship[0]["COU"]) and count($getScholarship[0]["COU"])) {
			$e2cText = "";
			foreach($getScholarship[0]["COU"] as $couData) {
				$e2cText .= (($e2cText)?(", "):($e2cText)) . anchor($couData["conUrlName"]."/".$couData["couUrlName"], $couData["couName"]);
			}
			$getEnt2cat[$this->lang->line("CANDIDATE_MUST_BE_FROM")] = array(
				"e2cText" => $e2cText,
				"e2cInt" => NULL,
				"catName" => $this->lang->line("CANDIDATE_MUST_BE_FROM"),
				"catType" => "C",
			);
		}
		
		if(isset($getEnt2cat[$this->config->item("categorySCHType")]) and strtolower($getEnt2cat[$this->config->item("categorySCHType")]["e2cText"]) == "other" and isset($getEnt2cat[$this->config->item("categorySCHTypeOther")])) {
			$getEnt2cat[$this->config->item("categorySCHType")]["e2cText"] = $getEnt2cat[$this->config->item("categorySCHTypeOther")]["e2cText"];
		}
		unset($getEnt2cat[$this->config->item("categorySCHTypeOther")]);
		
		if(isset($getEnt2cat[$this->config->item("categorySCHDuration")]) and strtolower($getEnt2cat[$this->config->item("categorySCHDuration")]["e2cText"]) == "other" and isset($getEnt2cat[$this->config->item("categorySCHDurationOther")])) {
			$getEnt2cat[$this->config->item("categorySCHDuration")]["e2cText"] = $getEnt2cat[$this->config->item("categorySCHDurationOther")]["e2cText"];
		}
		unset($getEnt2cat[$this->config->item("categorySCHDurationOther")]);
	}

	// MAJ adatok befűzése
	if($ENTITY == "MAJ" and isset($getMajor)) {
		//common::pr($getEnt2cat);
		$tmp_getEnt2cat = array();
		$tmp_getEnt2cat[$this->lang->line("SUBJECT_AREA_OF_THE_COURSE")] = array(
			"e2cText" => $getMajor[0]["subName"],
			"e2cInt" => NULL,
			"catName" => $this->lang->line("SUBJECT_AREA_OF_THE_COURSE"),
			"catType" => "C",
		);
		$tmp_getEnt2cat[$this->lang->line("TYPE_OF_THE_COURSE")] = array(
			"e2cText" => $getMajor[0]["majProgTypeLname"],
			"e2cInt" => NULL,
			"catName" => $this->lang->line("TYPE_OF_THE_COURSE"),
			"catType" => "C",
		);
		$tmp_getEnt2cat[$this->lang->line("PROGRAM_TYPE")] = array(
			"e2cText" => $getMajor[0]["majProgType2Lname"],
			"e2cInt" => NULL,
			"catName" => $this->lang->line("PROGRAM_TYPE"),
			"catType" => "C",
		);
		$tmp_getEnt2cat2 = array();
		foreach($tmp_getEnt2cat as $catId => $catData) {
			$tmp_getEnt2cat2[$catId] = $catData;
		}
		foreach($getEnt2cat as $catId => $catData) {
			$tmp_getEnt2cat2[$catId] = $catData;
		}
		$getEnt2cat = $tmp_getEnt2cat2;
		unset($tmp_getEnt2cat);
		unset($tmp_getEnt2cat2);
		
		if(isset($getEnt2cat[$this->config->item("categoryMAJTrainingLevelId")]) and strtolower($getEnt2cat[$this->config->item("categoryMAJTrainingLevelId")]["e2cText"]) == "other" and isset($getEnt2cat[$this->config->item("categoryMAJTrainingLevelOtherId")])) {
			$getEnt2cat[$this->config->item("categoryMAJTrainingLevelId")]["e2cText"] = $getEnt2cat[$this->config->item("categoryMAJTrainingLevelOtherId")]["e2cText"];
		}
		unset($getEnt2cat[$this->config->item("categoryMAJTrainingLevelOtherId")]);

		if(isset($getEnt2cat[$this->config->item("categoryMAJLanguageId")]) and strtolower($getEnt2cat[$this->config->item("categoryMAJLanguageId")]["e2cText"]) == "other" and isset($getEnt2cat[$this->config->item("categoryMAJLanguageOtherId")])) {
			$getEnt2cat[$this->config->item("categoryMAJLanguageId")]["e2cText"] = $getEnt2cat[$this->config->item("categoryMAJLanguageOtherId")]["e2cText"];
		}
		unset($getEnt2cat[$this->config->item("categoryMAJLanguageOtherId")]);
	}
	
	// INS adatok befűzése
	if($ENTITY == "INS" and isset($getInstitute)) {
		$tmp_getEnt2cat = array();
		if(isset($getEnt2cat[$this->config->item("categoryINSAddress")])) {
			$tmp_getEnt2cat[$this->lang->line("ADDRESS")] = array(
				"e2cText" => $getEnt2cat[$this->config->item("categoryINSAddress")]["e2cText"].", ".$getInstitute[0]["cityName"].((isset($getEnt2cat[$this->config->item("categoryINSPostalCode")]))?(", ".$getEnt2cat[$this->config->item("categoryINSPostalCode")]["e2cInt"]):("")),
				"e2cInt" => NULL,
				"catName" => $this->lang->line("ADDRESS"),
				"catType" => "C",
			);
		}
	
		if(isset($getEnt2cat[$this->config->item("categoryINSPostalCode")])) {
			unset($getEnt2cat[$this->config->item("categoryINSPostalCode")]);
		}
		if(isset($getEnt2cat[$this->config->item("categoryINSAddress")])) {
			unset($getEnt2cat[$this->config->item("categoryINSAddress")]);
		}
		
		$tmp_getEnt2cat2 = array();
		foreach($tmp_getEnt2cat as $catId => $catData) {
			$tmp_getEnt2cat2[$catId] = $catData;
		}
		foreach($getEnt2cat as $catId => $catData) {
			$tmp_getEnt2cat2[$catId] = $catData;
		}
		$getEnt2cat = $tmp_getEnt2cat2;
		unset($tmp_getEnt2cat);
		unset($tmp_getEnt2cat2);
	}

	// ACC adatok befűzése
	if($ENTITY == "ACC" and isset($getAccomodation)) {
		$tmp_getEnt2cat = array();
		if(isset($getEnt2cat[$this->config->item("categoryACCAddress")])) {
			$tmp_getEnt2cat[$this->lang->line("ADDRESS")] = array(
				"e2cText" => $getEnt2cat[$this->config->item("categoryACCAddress")]["e2cText"].", ".$getAccomodation[0]["cityName"].((isset($getEnt2cat[$this->config->item("categoryACCPostalCode")]))?(", ".$getEnt2cat[$this->config->item("categoryACCPostalCode")]["e2cInt"]):("")),
				"e2cInt" => NULL,
				"catName" => $this->lang->line("ADDRESS"),
				"catType" => "C",
			);
		}
	
		if(isset($getEnt2cat[$this->config->item("categoryACCPostalCode")])) {
			unset($getEnt2cat[$this->config->item("categoryACCPostalCode")]);
		}
		if(isset($getEnt2cat[$this->config->item("categoryACCAddress")])) {
			unset($getEnt2cat[$this->config->item("categoryACCAddress")]);
		}
		
		$tmp_getEnt2cat2 = array();
		foreach($tmp_getEnt2cat as $catId => $catData) {
			$tmp_getEnt2cat2[$catId] = $catData;
		}
		foreach($getEnt2cat as $catId => $catData) {
			$tmp_getEnt2cat2[$catId] = $catData;
		}
		$getEnt2cat = $tmp_getEnt2cat2;
		unset($tmp_getEnt2cat);
		unset($tmp_getEnt2cat2);
	}
	
	$maxLastupd = "";
	foreach($getEnt2cat as $catId => $catData) {
		if(isset($catData["e2cLastupd"]) and $catData["e2cLastupd"] > $maxLastupd) {
			$maxLastupd = $catData["e2cLastupd"];
		}
		// SCH amount ugrás
		if(in_array($catId, array($this->config->item("categorySCHAmountCurrencyId"), $this->config->item("categorySCHAmountIntervalId")))) {
			continue;
		}
		// MAJ fee ugrás
		if(in_array($catId, array($this->config->item("categoryMAJFeeCurrencyId"), $this->config->item("categoryMAJFeeIntervalId")))) {
			continue;
		}
		// ACC amount ugrás
		if(in_array($catId, array($this->config->item("categoryACCFeeCurrencyId"), $this->config->item("categoryACCFeeIntervalId")))) {
			continue;
		}
		if(in_array($catData["catType"], array("I", "C", "L", "D"))) {
			// SCH amount
			if($catId == $this->config->item("categorySCHAmountId")) {
				$catData["e2cText"] = Common::numericFormat(Common::coalesce(array($catData["e2cInt"], $catData["e2cText"])));
				$catData["e2cInt"] = NULL;
				if(isset($getEnt2cat[$this->config->item("categorySCHAmountCurrencyId")])) {
					$catData["e2cText"] .= " ".Common::coalesce(array($getEnt2cat[$this->config->item("categorySCHAmountCurrencyId")]["e2cInt"], $getEnt2cat[$this->config->item("categorySCHAmountCurrencyId")]["e2cText"]));
				}
				if(isset($getEnt2cat[$this->config->item("categorySCHAmountIntervalId")])) {
					$catData["e2cText"] .= " / ".Common::coalesce(array($getEnt2cat[$this->config->item("categorySCHAmountIntervalId")]["e2cInt"], $getEnt2cat[$this->config->item("categorySCHAmountIntervalId")]["e2cText"]));
				}
			}
			// ACC amount
			if($catId == $this->config->item("categoryACCFeeId")) {
				$catData["e2cText"] = Common::numericFormat(Common::coalesce(array($catData["e2cInt"], $catData["e2cText"])));
				$catData["e2cInt"] = NULL;
				if(isset($getEnt2cat[$this->config->item("categoryACCFeeCurrencyId")])) {
					$catData["e2cText"] .= " ".Common::coalesce(array($getEnt2cat[$this->config->item("categoryACCFeeCurrencyId")]["e2cInt"], $getEnt2cat[$this->config->item("categoryACCFeeCurrencyId")]["e2cText"]));
				}
				if(isset($getEnt2cat[$this->config->item("categoryACCFeeIntervalId")])) {
					$catData["e2cText"] .= " / ".Common::coalesce(array($getEnt2cat[$this->config->item("categoryACCFeeIntervalId")]["e2cInt"], $getEnt2cat[$this->config->item("categoryACCFeeIntervalId")]["e2cText"]));
				}
			}
			// MAJ fee
			if($catId == $this->config->item("categoryMAJFeeId")) {
				$catData["e2cText"] = Common::numericFormat(Common::coalesce(array($catData["e2cInt"], $catData["e2cText"])));
				$catData["e2cInt"] = NULL;
				if(isset($getEnt2cat[$this->config->item("categoryMAJFeeCurrencyId")])) {
					$catData["e2cText"] .= " ".Common::coalesce(array($getEnt2cat[$this->config->item("categoryMAJFeeCurrencyId")]["e2cInt"], $getEnt2cat[$this->config->item("categoryMAJFeeCurrencyId")]["e2cText"]));
				}
				if(isset($getEnt2cat[$this->config->item("categoryMAJFeeIntervalId")])) {
					$catData["e2cText"] .= " / ".Common::coalesce(array($getEnt2cat[$this->config->item("categoryMAJFeeIntervalId")]["e2cInt"], $getEnt2cat[$this->config->item("categoryMAJFeeIntervalId")]["e2cText"]));
				}
			}
			$page["sideinfo"][] = $catData;
		} elseif(count($page["summary"]) < 1) {
			$page["summary"][] = $catData;
		} else {
			$page["details"][] = $catData;
		}
	}
	
	if(!(count($page["sideinfo"]) or $ENTITY == "INS" or ($ENTITY == "CITY" and isset($getInstitute) and count($getInstitute)))) unset($page["sideinfo"]);

	//Common::pr($page); exit;

	$catStep = 0;
	$imgScroller = "";
	echo	"<div class=\"fl\" style=\"width: 620px;\">";
	foreach($page as $subPage => $subData) {
		if(count($page[$subPage]) or $subPage == "sideinfo" or (isset($getCityInstitute) and count($getCityInstitute))) {
			switch($subPage) {
				case "sideinfo": {

					if(!$catStep and isset($page["summary"][0])) {
						echo	"<h1>".$page["summary"][0]["catName"]."</h1>";
					}

					echo	"<aside class=\"box gray sideinfo\">";
					echo		"<div class=\"boxcontent\">";
					if(count($subData))  {
						echo			"<h3>".((isset($entName))?($entName):($this->lang->line("FAST_FACTS")))."</h3>";
						echo			"<dl>";
						foreach($subData as $catData) {
							echo				"<dt>".$catData["catName"].":</dt>";
							echo				"<dd>".Common::coalesce(array(Common::numericFormat($catData["e2cInt"]), auto_link($catData["e2cText"], "both", TRUE)))."</dd>";
						}
						echo			"</dl>";
					}

					if(!function_exists("recursive_top_institute_print")) {
						function recursive_top_institute_print(&$arr, $parent) {
							echo	"<ul>";
							foreach($arr as $jj => $insData) {
								if($insData["insInsId"] == $parent) {
									$insId = $arr[$jj]["insId"];
									echo		"<li>".anchor("institutes/".$insData["insUrlName"], $insData["insName"]);
									unset($arr[$jj]);
									if(count($arr)) {
										recursive_top_institute_print($arr, $insId);
									}
								}
								echo	"</li>";
							}
							echo	"</ul>";
						}
					}
					
					if(isset($getCityInstitute) and count($getCityInstitute)) {
						//Common::pr($getCityInstitute);
						if(count($page["sideinfo"])) {
							echo			"<hr/>";
						}
						echo			"<h3>".$this->lang->line("TOP_CITIES_UNIVERSITIES")."</h3>";
						echo			"<ul class=\"cols masonry\">";
						foreach($getCityInstitute as $cityData) {
							echo				"<li class=\"masonry-brick\">";
							echo					anchor($cityData["conUrlName"]."/".$cityData["couUrlName"]."/".$cityData["cityUrlName"], $cityData["cityName"], "", $forced = TRUE);
							if(isset($cityData["ins"])) {
								$arr = $cityData["ins"];
								recursive_top_institute_print($arr, "");
								if(count($arr)) {
									reset($arr);
									while(count($arr)) {
										$curr = current($arr);
										recursive_top_institute_print($arr, $curr["insInsId"]);
									}
								}
							}
							echo				"</li>";
						}
						echo			"</ul>";
					}

					if($ENTITY != "INS" and isset($getInstitute) and count($getInstitute)) {
						if(count($page["sideinfo"])) {
							echo			"<hr/>";
						}
						echo			"<h3>".$this->lang->line("TOP_UNIVERSITIES")."</h3>";
						//Common::pr($getInstitute);
						$arr = $getInstitute;
						recursive_top_institute_print($arr , "");
						if(count($arr)) {
							reset($arr);
							while(count($arr)) {
								$curr = current($arr);
								recursive_top_institute_print($arr, $curr["insInsId"]);
							}
						}
					}
					
					if($ENTITY == "INS" and count($getChildren)) {
						function recursive_institute_print($arr) {
							echo	"<ul>";
							foreach($arr as $insData) {
								echo		"<li>".anchor("institutes/".$insData["insUrlName"], $insData["insName"]);
								if(isset($insData["children"])) {
									recursive_institute_print($insData["children"]);
								}
								echo	"</li>";
							}
							echo	"</ul>";
						}
						//Common::pr($getChildren);
						if(count($page["sideinfo"])) {
							echo			"<hr/>";
						}
						echo			"<h3>".$this->lang->line((count($getChildren) > 1)?("FACULTIES"):("FACULTY"))."</h3>";
						recursive_institute_print($getChildren);
					}

					if(in_array($ENTITY, array("INS", "MAJ"))) {
						if(count($page["sideinfo"])) {
							echo			"<hr/>";
						}
						//((count($getAccomodation) > 1)?("s"):(""))
						if(count($getAccomodation)) {
							echo			"<h3>".$this->lang->line("CHOOSE_ACCOMODATION")."</h3>";
							echo			"<ul>";
							foreach($getAccomodation as $accData) {
								echo				"<li>".anchor("accomodations/".$accData["accUrlName"], $accData["accName"]);
							}
							echo			"</ul>";
						} else {
							echo			"<h3>".$this->lang->line("ACCOMODATION")."</h3>";
							echo			"<p>".$this->lang->line("THERE_IS_NO_ACCOMODATION_UPLOADED")."</p>";
						}
					}
					
					echo		"</div>";

					// institute form
					if($ENTITY == "INS") { ?>
						<hr/>
						<? if(!$this->session->userdata("usrId")) { ?>
							<h3><?=$this->lang->line("QUESTIONS_LOG_IN_AND_CONTACT_THE_INSTITUTE")?></h3>
						<? } else { ?>
							<h3><?=$this->lang->line("QUESTIONS_CONTACT_THE_INSTITUTE")?></h3>
							<?=$contactForm?>
						<? } ?>
					<?}

					if($maxLastupd) {
						echo		"<p class=\"fr\"><i>".$this->lang->line("LAST_UPDATE_AT").": ".Common::dateFromDatabase($maxLastupd)."</i></p>";
					}
					
					echo	"</aside>";
				} break;
				case "details":
				case "summary": {
					echo	"<div class=\"".$subPage."\" ".(($subPage == "details")?("style=\"display:none;\""):("")).">";
					foreach($subData as $ii => $catData) {
						if($ii or $subPage == "details") {
							echo		"<hr/>";
						}
						//if(($subPage == "summary" and $ii) or $subPage == "details" or $ENTITY == "CON") {
						//if(($subPage == "summary" and ($ii or !count($page["sideinfo"]))) or $subPage == "details") {
						if($catStep or !isset($page["sideinfo"])) {
							echo				"<h1>".$catData["catName"]."</h1>";
						}
						echo				"<div>".Common::coalesce(array(Common::numericFormat($catData["e2cInt"]), auto_link($catData["e2cText"], "both", TRUE)))."</div>";
						$catStep++;
					}
					echo	"</div>";

					if($subPage == "details" and count($subData)) {
						echo	"<div class=\"pager\"><a class=\"more\" href=\"#\">".ucfirst($this->lang->line("MORE"))."</a></div>";
					}

					if(!$imgScroller and isset($getEnt2image) and count($getEnt2image)) {
						$imgScroller .=	"<div id=\"carousel\" class=\"image_carousel\" style=\"width:620px;\">";
						$imgScroller .=		"<div id=\"foo2\" style=\"text-align:center;\">";
						foreach($getEnt2image as $ii => $image) {
							//list($width, $height, $type, $attr) = getimagesize(base_url().UPLOADS.$image["e2iImageNameThumb"]);
							$imgScroller .=			"<a href=\"".UPLOADS.$image["e2iImageNameResized"]."\" class=\"lightbox\" title=\"".$image["e2iAlt"]."\"><img src=\"".UPLOADS.$image["e2iImageNameThumb"]."\" alt=\"".$image["e2iAlt"]."\" height=\"".$image["e2iThumbHeight"]."\" width=\"".$image["e2iThumbWidth"]."\" id=\"id".$image["e2iId"]."\" /></a>";
						}
						$imgScroller .=		"</div>";
						$imgScroller .=		"<div class=\"clearfix\"></div>";
						//$imgScroller .=		"<a class=\"prev\" id=\"foo2_prev\" href=\"#\"><span>".$this->lang->line("PREV")."</span></a>";
						//$imgScroller .=		"<a class=\"next\" id=\"foo2_next\" href=\"#\"><span>".$this->lang->line("NEXT")."</span></a>";
						$imgScroller .=		"<div class=\"pagination\" id=\"foo2_pag\"></div>";
						$imgScroller .=	"</div>";
					}

				}
			}
		}
	}
	if(!((isset($page["sideinfo"]) and count($page["sideinfo"])) or (isset($page["summary"]) and count($page["summary"])) or (isset($page["details"]) and count($page["details"])))) {
		echo		"<p>".$this->lang->line("THIS_PAGE_UNDER_CONSTRUCTION")."</p>";
	}
	echo	"</div>";

	if($imgScroller) {
		echo 	"<div class=\"carousel_clearfix\"></div>";
		echo	$imgScroller;

		?>
		<!-- Plugin initialization -->
		<script type="text/javascript">
			$(document).ready(function () {

				$("#foo2").carouFredSel({
					circular: true,
					infinite: true,
					auto : {
						play: true,
						interval: 50,
						pauseOnHover: true
					},
					items: {
						visible: <?=((count($getEnt2image) > 5)?(5):(3))?>,
						start: 0
					},					
					swipe  : {
						onTouch: true,
						onMouse: true
					},
					mousewheel: {
					},
					scroll: {
						items: 1,
						pauseOnHover: true
					},
					prev	: {	
						<? /*button	: "#foo2_prev",*/ ?>
						key		: "left"
					},
					next	: { 
						<? /*button	: "#foo2_next", */ ?>
						key		: "right"
					},
					pagination	: {
						container		: "#foo2_pag",
						anchorBuilder	: function( nr ) {
							return "<a><span>" + nr + "</span></a>";
						}
					}

					
				});

				<? /*
				$("div#carousel").carouFredSel({
					circular: true,
					infinite: true,
					width: 620,
					auto: {
						items 			: 1,
						duration		: 5000,
						easing			: "linear",
						timeoutDuration	: 0,
						pauseOnHover	: "immediate"
					}
					//,pagination  : "div#carousel_pagination"
				}); */
				

				/*
				$(window).bind("resize", function () {
					self._showHideHotSpots();
					self.recalculateHotSpotsPos();
					self._trigger("windowResized");
				});
				*/

				/*
				$('div#makeMeScrollable').smoothDivScroll({
					mousewheelScrolling: 'allDirections',
					hiddenOnStart: false,
					manualContinuousScrolling: true
				});
				*/				
				?>

				$('a.lightbox').lightBox({
					imageLoading: '<?=MEDIA?>lightbox/images/lightbox-ico-loading.gif',
					imageBtnClose: '<?=MEDIA?>lightbox/images/lightbox-btn-close.gif',
					imageBtnPrev: '<?=MEDIA?>lightbox/images/lightbox-btn-prev.gif',
					imageBtnNext: '<?=MEDIA?>lightbox/images/lightbox-btn-next.gif'
				});
			});
		</script>
		<?

	}

}